package com.tx.utils.interceptor;

import com.tx.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author: tcx
 * @description: 对管理员管理界面进行拦截
 * @Date: 2019/11/10
 **/
public class TeacherMangerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String url = httpServletRequest.getRequestURI();
        HttpSession session = httpServletRequest.getSession();
        //判断是否是管理员，是管理员则放行
        User user = (User) session.getAttribute("user");
        if (user!=null&&"admin".equals(user.getuType())){
            return true;
        }
        if (url.toLowerCase().indexOf("teacher")>0 || url.toLowerCase().indexOf("admin")>0 ){
            //判断是否登录，登录则放行
            if (session.getAttribute("user")!=null){
                    if ("admin".equals(user.getuType())){
                        return true;
                    }else if("老师".equals(user.getuType())){
                        if(url.toLowerCase().indexOf("admin")>0){
                            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/user/quanxian.do");
                            return false;
                        }
                        return true;
                    }else{
                        httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/user/quanxian.do");
                        return false;
                    }
            }else{
                httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/user/to_login.do");
                return false;
            }

        }
        httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/user/quanxian.do");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
